import dayjs from "dayjs"

//平台判断
export function isIos() {
	return uni.getDeviceInfo().platform === "ios"
}
//格式化日期
export function dataToDate(date: string, type?: number) {
	if (!date) return "-"
	if (date === "1970-01-01 00:00:00" || date === "1970-01-01") return "-"
	if (type === 1) {
		return dayjs(date).format("YYYY-MM-DD")
	} else if (type === 2) {
		return dayjs(date).format("YYYY-MM-DD HH:mm:ss")
	} else if (type === 3) {
		return dayjs(date).format("YYYY-MM-DD HH:mm")
	} else if (type === 4) {
		return dayjs(date).format("MM-DD")
	} else {
		return date
	}
}
// 开园时间
export const formatTime = (time?: string) => {
	if (time) {
		const arr = time.split(":")

		return arr.slice(0, 2).join(":")
	}

	return ""
}
// 字符串截取
export function subString(str: string, len = 6, hasDot = true) {
	if (str.length > len) {
		return str.substring(0, len) + (hasDot ? "..." : "")
	}
	return str
}

//日期特殊化
export function dataToToday(date: string) {
	if (!date) return "-"
	if (date === "1970-01-01 00:00:00" || date === "1970-01-01") return "-"
	const today = dayjs().startOf("day")
	const yesterday = today.subtract(1, "day").startOf("day")
	const givenTime = dayjs(date)
	if (givenTime.isSame(today, "day")) {
		return "今天  " + dayjs(date).format("HH:mm")
	} else if (givenTime.isSame(yesterday, "day")) {
		return "昨天  " + dayjs(date).format("HH:mm")
	} else {
		return dayjs(date).format("MM-DD HH:mm")
	}
}
// 格式化时间
export function formatHourMinute(str: string) {
	return dayjs("1970-01-01 " + str).format("HH:mm")
}
//金额处理
export function priceInCent(num = 0, fixed = 2) {
	return num ? Number((+num / 100).toFixed(fixed)) : 0
}
//评分处理
export function scoreInCent(num = 0, fixed = 1) {
	return num ? (+num / 100).toFixed(fixed) : 0
}
// 公里数
export function distanceInKm(num = 0, fixed = 1) {
	return num ? (+num / 1000).toFixed(fixed) : 0
}
//手机号脱敏
export function formatPhone(str: string) {
	let reg = /(\d{3})\d{4}(\d{4})/ //正则表达式
	let phone = str.replace(reg, "$1****$2")
	return phone
}
//身份证脱敏
export function formatIdCard(idCard: string) {
	if (idCard.length == 15) {
		return idCard.replace(/(\d{6})\d{6}(\d{3})/, "$1******$2")
	} else {
		return idCard.replace(/(\d{5})\d{6}(\d{6})/, "$1******$2")
	}
}
//空数据格式化占位符
export function nullToSymbol(val: string) {
	return [null, "", undefined].includes(val) ? "-" : val
}
//input校验输入正整数
export function inputFormatInteger(value: string, formData: any, prop: string, max: number, min: number) {
	let result = value.replace(/[^\d]/g, "")
	if ((max || max === 0) && Number(result) > max) {
		result = max as any
	}
	if ((min || min === 0) && Number(result) < min) {
		result = min as any
	}
	formData[prop] = result
}
//input校验输入小数
export function inputFormatFixed(
	value: string,
	formData: any,
	prop: string,
	fixed: number = 2,
	max: number,
	min: number
) {
	if (!new RegExp(`^\\d+(\\.\\d{0,${fixed}})?$`).test(value)) {
		// 如果不符合规则，就截取前面符合规则的部分
		let result = value.match(new RegExp(`^\\d+(\\.\\d{0,${fixed}})?`))?.[0]
		if ((max || max === 0) && Number(result) > max) {
			result = max as any
		}
		if ((min || min === 0) && Number(result) < min) {
			result = min as any
		}
		formData[prop] = result
	} else {
		if ((max || max === 0) && Number(value) > max) {
			formData[prop] = max
		}
		if ((min || min === 0) && Number(value) < min) {
			formData[prop] = min
		}
	}
}
//拨打电话
export function phoneCall(phoneNumber: string) {
	phoneNumber &&
		uni.makePhoneCall({
			phoneNumber,
		})
}
// 格式化日期区间
export function tourDateText(startDate: string, endDate: string) {
	if (startDate === endDate) return startDate
	return `${startDate} 至 ${endDate}`
}
// 当销量在 100 以内时，精确显示具体销量数字；超过 100 但不足 1000 时，显示 “99+”；超过 1000 但不足 1 万时，显示 “999+”；达到或超过1万时，则显示 “x万+”
export function formatSaleQuantity(quantity: number) {
	if (quantity < 100) {
		return quantity
	} else if (quantity < 1000) {
		return "99+"
	} else if (quantity < 10000) {
		return "999+"
	} else {
		let tenThousandMultiple = Math.floor(quantity / 10000)
		return tenThousandMultiple + "万+"
	}
}
